﻿/* dk2Repository - Copyright (C) 2011 Daniel Kuczewski http://www.dk2.de/

 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

using System;
using System.Data;
using System.Text;

namespace dk2Repository.QueryBuilder.SqLite
{
    internal class SqLiteCreateTableBuilder : dk2Repository.QueryBuilder.CreateTableBuilder
    {

        public override string CreateSql()
        {
            var sb = new StringBuilder();
            sb.Append("CREATE TABLE ").Append(CreateIfNotExists ? "IF NOT EXISTS " : "").Append(Table).Append(" (");

            foreach (var column in Columns)
            {
                sb.Append(column.Name)
                    .Append(" ")
                    .Append(GetSqlType(column.Type, column.Length))
                    .Append(" ")
                    .Append(column.Pk ? "PRIMARY KEY" : "")
                    .Append(" ")
                    .Append(column.Autoinc ? "AUTOINCREMENT" : "")
                    .Append(" ")
                    .Append(column.Nullable ? "" : "NOT NULL")
                    .Append(",");
            }

            sb.Remove(sb.Length - 1, 1);
            sb.Append(");");

            return sb.ToString();
        }

        private static string GetSqlType(DbType type, uint length)
        {
            switch (type)
            {
                case DbType.AnsiString:
                    return "TEXT";
                case DbType.AnsiStringFixedLength:
                    return "TEXT";
                case DbType.Boolean:
                    return "INTEGER";
                case DbType.Currency:
                    return "REAL";
                case DbType.Date:
                    return "TEXT";
                case DbType.DateTime:
                    return "TEXT";
                case DbType.Decimal:
                    return "REAL";
                case DbType.Double:
                    return "REAL";
                case DbType.Guid:
                    return "TEXT";
                case DbType.Int16:
                    return "INTEGER";
                case DbType.Int32:
                    return "INTEGER";
                case DbType.Int64:
                    return "INTEGER";
                case DbType.String:
                    return "TEXT";
                case DbType.StringFixedLength:
                    return "TEXT";
                case DbType.UInt16:
                    return "INTEGER";
                case DbType.UInt32:
                    return "INTEGER";
                case DbType.UInt64:
                    return "INTEGER";

                default:
                    throw new Exception(type.ToString() + "not supported.");
            }

        }


    }
}